package com.sino.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sino.operation.commons.commonEnum.TaskState;
import com.sino.operation.entity.BizTaskPool;
import com.sino.operation.entity.SysAbMessage;
import com.sino.operation.mapper.BizTaskPoolMapper;
import com.sino.operation.service.BizTaskPoolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务池 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-10-07
 */
@Service
public class BizTaskPoolServiceImpl extends ServiceImpl<BizTaskPoolMapper, BizTaskPool> implements BizTaskPoolService {

    @Autowired(required = false)
    private BizTaskPoolMapper poolMapper;

    @Override
    public SysAbMessage getMessageByTellerCode(String tellerCode) {
        return poolMapper.getMessageOneByTellerCode(TaskState.TaskState3.getValue(), tellerCode);
    }

    @Override
    public SysAbMessage getMessageByRelease(String tellerPost, String tellerCode) {
        return poolMapper.getMessageOneByRelease(TaskState.TaskState1.getValue(), tellerPost, tellerCode);
    }

    @Override
    public SysAbMessage getMessageByNotRelease(String tellerPost, String tellerCode) {
        return poolMapper.getMessageOneByNotRelease(TaskState.TaskState1.getValue(), tellerPost, tellerCode);
    }

    @Override
    public void updateTaskState(String taskId, String state, boolean flag, String tellerCode) {
        QueryWrapper<BizTaskPool> wrapper = new QueryWrapper<>();
        if (taskId != null && !taskId.isEmpty()) {
            wrapper.eq("TASK_ID", taskId);
        }
        BizTaskPool data = this.getOne(wrapper);
        data.setTaskState(state);
        if (!flag) {
            //flag为false,将柜员置空
            data.setTellerCode(null);
        } else if (tellerCode != null) {
            //flag为true,且柜员号不为空，将柜员号从新赋值
            data.setTellerCode(tellerCode);
        }
        this.saveOrUpdate(data);
    }

    @Override
    public void updateNotTellerCode(String journalNo, String taskId, String tellerCode) {
        QueryWrapper<BizTaskPool> wrapper = new QueryWrapper<>();
        if (journalNo != null && taskId != null) {
            wrapper.eq("JOURNAL_NO", journalNo);
            wrapper.ne("TASK_ID", taskId);
            BizTaskPool bizTaskPool = this.getOne(wrapper);
            bizTaskPool.setNotTellerCode(tellerCode);
            this.saveOrUpdate(bizTaskPool);
        }
    }

    @Override
    public BizTaskPool selectTaskByJournalNo(String journalNo) {
        QueryWrapper<BizTaskPool> wrapper = new QueryWrapper<>();
        if (journalNo != null && !journalNo.isEmpty()) {
            wrapper.eq("JOURNAL_NO", journalNo);
        }
        BizTaskPool bizTaskPool = this.getOne(wrapper);
        return bizTaskPool;
    }

    @Override
    public List<BizTaskPool> selectList(String journalNo) {
        QueryWrapper<BizTaskPool> wrapper = new QueryWrapper<>();
        wrapper.eq("JOURNAL_NO",journalNo);
        List<BizTaskPool> bizTaskPools = poolMapper.selectList(wrapper);
        return bizTaskPools;
    }


}
